package com.ywgt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.Admin;
import com.ywgt.pojo.Brand;
import com.ywgt.mapper.BrandMapper;
import com.ywgt.pojo.Category;
import com.ywgt.service.AdminService;
import com.ywgt.service.BrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.service.CategoryService;
import com.ywgt.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ywgt.pojo.CategoryVo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌商表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    CategoryService categoryService;
    @Autowired
    AdminService adminService;

    @Override
    public Page<Brand> getBrands(String name, Integer id, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectBrands(name,id,sort,order);
    }

    @Override
    public Object catAndAdmin() {
        List<Category> categories = categoryService.getMenu();
        List<Admin> admins = adminService.getAll();
        List<Map<String, Object>> adminList = new ArrayList<>(admins == null ? 0 : admins.size());
        for (Admin admin : admins) {
            Map<String, Object> b = new HashMap<>(2);
            b.put("value", admin.getId());
            b.put("label", admin.getUsername());
            adminList.add(b);
        }
        List<CategoryVo> categoryVos=new ArrayList<>();
        for (Category category : categories) {
            CategoryVo categoryVo=new CategoryVo();
            categoryVo.setValue(category.getId());
            categoryVo.setLabel(category.getName());
            List<CategoryVo> childrens=new ArrayList<>();
            List<Category> child = category.getChild();
            for (Category children : child) {
                CategoryVo chi=new CategoryVo();
                chi.setValue(children.getId());
                chi.setLabel(children.getName());
                childrens.add(chi);
            }
            categoryVo.setChildren(childrens);
            categoryVos.add(categoryVo);
        }
        Map<String,Object> data=new HashMap<>();
        data.put("categoryList",categoryVos);
        data.put("adminList",adminList);
        return ResponseUtil.ok(data);
    }


}
